package com.yuliang.ai.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.yuliang.ai.domain.Dto.UserInfoDto;
import com.yuliang.ai.domain.Vo.UserInfoVo;
import com.yuliang.ai.request.RequestUrlUtils;
import com.yuliang.ai.service.UserInfoService;
import com.yuliang.utils.result.ResultUtils;
import com.yuliang.utils.result.emums.HttpCodeEnum;
import com.yuliang.utils.util.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Override
    public String getUserInfo(UserInfoVo userInfoVo) {
        Map<String, String> header = new HashMap<>();
        header.put("tenant-id", "1");
        header.put("Authorization", userInfoVo.getToken());
        header.put("Content-Type", "application/json");
        String result = HttpRequestUtils.doGet("https://sjzy-appser.yjygx.com/api/app/user/getUserInfo", header);
        JSONObject jsonObject = JSONObject.parseObject(result);
        log.info("查询的值{}", jsonObject);
        if(jsonObject.getInteger("code") == 0){
            UserInfoDto userInfoDto = new UserInfoDto();
            userInfoDto.setUserName(jsonObject.getString("nickname"));
            userInfoDto.setUserId(jsonObject.getInteger("appUserId"));
            userInfoDto.setAvatarUrl(jsonObject.getString("avatarUrl"));
            return ResultUtils.ResultWithMessageDataEnum(HttpCodeEnum.select_success, userInfoDto);
        }
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.data_not_exist);
    }
}
